package ui.activity.mine.presenter;



import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import Bean.GetBankcardBean;
import base.BaseBiz;
import coom.Constants;
import ui.activity.mine.biz.BankCardBiz;
import ui.activity.mine.contract.BankCardContract;
import ui.activity.profit.ChangeBankAct;
import util.StringUtils;

/**
 * Created by chenjian on 19/2/26
 */

public class BankCardPresenter implements BankCardContract.Presenter {

    BankCardContract.View view;
    BankCardBiz bankCardBiz;
    List<GetBankcardBean.DataBean.ItemsBean> list = new ArrayList<>();


    @Inject
    public BankCardPresenter(BankCardContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        bankCardBiz = (BankCardBiz) biz;
    }

    @Override
    public void getCardList(boolean isRefresh) {
        list.clear();
        view.loading(true);
        bankCardBiz.getCardList(new BaseBiz.Callback<GetBankcardBean.DataBean>() {
            @Override
            public void onSuccess(GetBankcardBean.DataBean info) {
                list.addAll(info.getItems());
                view.upDateUI(list);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(GetBankcardBean.DataBean info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });
    }

    @Override
    public void goToChangeBank() {
        view.startAct(ChangeBankAct.class, (Serializable) list);
    }

    @Override
    public void deleteCard(String id, int position) {
        if (StringUtils.isEmpty(id)) {
            view.showMsg("请重新选择银行卡");
            return;
        }
        view.loading(true);
        String url = Constants.deleteCardUrl + id;
        bankCardBiz.deleteCard(url, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object info) {
                list.remove(position);
                view.upDateUI(list);
                view.loading(false);
            }

            @Override
            public void onFailure(Object info) {
                view.loading(false);
            }
        });
    }
}
